package cn.cttic.order.service.impl;

import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import cn.cttic.order.api.domain.bo.BaseInstallOrderMainPageBo;
import cn.cttic.order.api.domain.vo.BaseInstallOrderMainPageVo;
import cn.cttic.order.api.domain.BaseInstallOrderMainPage;
import cn.cttic.order.mapper.BaseInstallOrderMainPageMapper;
import cn.cttic.order.service.IBaseInstallOrderMainPageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 行驶证主页数据Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-10-22
 */
@RequiredArgsConstructor
@Service
public class BaseInstallOrderMainPageServiceImpl implements IBaseInstallOrderMainPageService {

    private final BaseInstallOrderMainPageMapper baseMapper;

    /**
     * 查询行驶证主页数据
     *
     * @param id 主键
     * @return 行驶证主页数据
     */
    @Override
    public BaseInstallOrderMainPageVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询行驶证主页数据列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 行驶证主页数据分页列表
     */
    @Override
    public TableDataInfo<BaseInstallOrderMainPageVo> queryPageList(BaseInstallOrderMainPageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseInstallOrderMainPage> lqw = buildQueryWrapper(bo);
        Page<BaseInstallOrderMainPageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的行驶证主页数据列表
     *
     * @param bo 查询条件
     * @return 行驶证主页数据列表
     */
    @Override
    public List<BaseInstallOrderMainPageVo> queryList(BaseInstallOrderMainPageBo bo) {
        LambdaQueryWrapper<BaseInstallOrderMainPage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseInstallOrderMainPage> buildQueryWrapper(BaseInstallOrderMainPageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseInstallOrderMainPage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOrderId()), BaseInstallOrderMainPage::getSaleOrderId, bo.getSaleOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getInstallOrderId()), BaseInstallOrderMainPage::getInstallOrderId, bo.getInstallOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOssId()), BaseInstallOrderMainPage::getOssId, bo.getOssId());
        lqw.eq(StringUtils.isNotBlank(bo.getOssUrl()), BaseInstallOrderMainPage::getOssUrl, bo.getOssUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleId()), BaseInstallOrderMainPage::getVehicleId, bo.getVehicleId());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BaseInstallOrderMainPage::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getIssuedBy()), BaseInstallOrderMainPage::getIssuedBy, bo.getIssuedBy());
        lqw.eq(StringUtils.isNotBlank(bo.getIssuedDate()), BaseInstallOrderMainPage::getIssuedDate, bo.getIssuedDate());
        lqw.eq(StringUtils.isNotBlank(bo.getBrandModel()), BaseInstallOrderMainPage::getBrandModel, bo.getBrandModel());
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleType()), BaseInstallOrderMainPage::getVehicleType, bo.getVehicleType());
        lqw.eq(StringUtils.isNotBlank(bo.getBelongTo()), BaseInstallOrderMainPage::getBelongTo, bo.getBelongTo());
        lqw.eq(StringUtils.isNotBlank(bo.getUsingProperty()), BaseInstallOrderMainPage::getUsingProperty, bo.getUsingProperty());
        lqw.eq(StringUtils.isNotBlank(bo.getEngineNo()), BaseInstallOrderMainPage::getEngineNo, bo.getEngineNo());
        lqw.eq(StringUtils.isNotBlank(bo.getCarNo()), BaseInstallOrderMainPage::getCarNo, bo.getCarNo());
        lqw.eq(StringUtils.isNotBlank(bo.getRegisterDate()), BaseInstallOrderMainPage::getRegisterDate, bo.getRegisterDate());
        return lqw;
    }

    /**
     * 新增行驶证主页数据
     *
     * @param bo 行驶证主页数据
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseInstallOrderMainPageBo bo) {
        BaseInstallOrderMainPage add = MapstructUtils.convert(bo, BaseInstallOrderMainPage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改行驶证主页数据
     *
     * @param bo 行驶证主页数据
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseInstallOrderMainPageBo bo) {
        BaseInstallOrderMainPage update = MapstructUtils.convert(bo, BaseInstallOrderMainPage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseInstallOrderMainPage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除行驶证主页数据信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
